from sys import exit
import binascii
from gmssl import sm3
import tkinter as tk

import mainchat
from certificate import *


def passwordcheck(username,password):
	
	loadlocaluser()#获取本地用户信息
	localtrustedusers()#加载受信任用户信息
	passwordlist=[i for i in password.encode('utf-8')]
	hashin=sm3.sm3_hash(passwordlist)
	hashreal=globalVal_cert.localuserpasswordhash
	
	if username==globalVal_cert.localID and hashin.lower()==hashreal.lower():
		logwin.destroy()
		mainchat.chatwin.deiconify()
		mainchat.chatwin.protocol("WM_DELETE_WINDOW",lambda:exit(0))
		mainchat.chatwin.mainloop()
	else:
		log_errorwin=tk.Toplevel()
		log_errorwin.title("错误")
		log_errorwin.geometry('200x50')
		p=tk.Label(log_errorwin,text="用户名或密码错误")
		p.pack()

mainchat.chatwin.withdraw()
logwin=tk.Tk()
logwin.geometry("330x150+500+400")
logwin.title("SLSchat 登录")

prompt0=tk.Label(logwin,text="\n  请输入本地用户名及其密码",font=('Default',13))
prompt1=tk.Label(logwin,text="  用户名",font=('Default',13))
prompt2=tk.Label(logwin,text="  密码",font=('Default',13))
e1=tk.Entry(logwin,width=20)
e2=tk.Entry(logwin,width=20)
logbutton=tk.Button(logwin,text=" 登录 ",command=lambda:passwordcheck(e1.get(),e2.get()),font=('Default',13))

prompt0.grid(row=0,column=0,columnspan=2)
prompt1.grid(row=1,column=0)
prompt2.grid(row=2,column=0)
e1.grid(row=1,column=1)
e2.grid(row=2,column=1)
logbutton.grid(row=3,column=0,columnspan=2)
logwin.protocol("WM_DELETE_WINDOW",lambda:exit(0))
logwin.mainloop()